home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / BOXES / HISTORY / HSTCBO.PAS < prev    next >
Pascal/Delphi Source File  |  1996-08-30  |  7KB  |  186 lines

  1. { -------------------------------------------------------------------------------------}
  2. { An "History ComboBox" component for Delphi32.                                        }
  3. { Copyright 1996, Christian Holzner.  All Rights Reserved.                             }
  4. { This component can be freely used and distributed in commercial and private          }
  5. { environments, provided this notice is not modified in any way.                       }
  6. { -------------------------------------------------------------------------------------}
  7. { Feel free to contact us if you have any questions, comments or suggestions at        }
  8. { cholzner@ping.at (Christian Holzner)                                                 }
  9. { JFConnault@mail.dotcom.fr (Jean-Fabien Connault)                                     }
  10. { -------------------------------------------------------------------------------------}
  11. { Date last modified:  08/13/96                                                        }
  12. { -------------------------------------------------------------------------------------}
  13.  
  14. { -------------------------------------------------------------------------------------}
  15. { THistoryComboBox v1.60                                                               }
  16. { -------------------------------------------------------------------------------------}
  17. { Description:                                                                         }
  18. {   A component that allows you to Read/Write Items of a TComboBox to/from Registry    }
  19. { Added Properties to ComboBox:                                                        }
  20. {   property Key: String;                    "Key in Registry"                         }
  21. {   property MaxHistoryLength: Integer;      "Max allowed length of the history-items  }
  22. {   property RootKey: TRootKey;                                                        }
  23. { Added Procedures to ComboBox:                                                        }
  24. {   procedure ReadRegistry;                  "Reads items from Registry"               }
  25. {   procedure WriteRegistry;                 "Writes items to Registry"                }
  26. {   procedure AddToList;                     "Adding an item to the Historylistbox"    }
  27. {                                                                                      }
  28. { See example contained in example.zip file for more details.                          }
  29. { -------------------------------------------------------------------------------------}
  30. { Revision History:                                                                    }
  31. { 1.00:  + Uses Ini-file for storage by Christian Holzner (cholzner@ping.at)           }                                                             
  32. { 1.50:  + Uses Registry for storage by Christian Holzner (cholzner@ping.at)           }
  33. { 1.60:  + Added RootKey property by Jean-Fabien Connault (JFConnault@mail.dotcom.fr)  }
  34. { -------------------------------------------------------------------------------------}
  35.  
  36. unit Hstcbo;
  37.  
  38. interface
  39.  
  40. uses
  41.   SysUtils, Windows, Classes, StdCtrls, Forms, Registry;
  42.  
  43. type
  44.  TRootKey = (hkClassesRoot, hkCurrentConfig, hkCurrentUser, hkDynData,
  45.              hkLocalMachine,hkUsers);
  46.  
  47.  THistoryComboBox = class(TComboBox)
  48.   private
  49.     FKey: String;
  50.     FMaxHistoryLength: Integer;
  51.     FRootKey: TRootKey;
  52.     procedure SetRootKey(ARootKey: TRootKey);
  53. protected
  54.     { Protected-Deklarationen }
  55.   public
  56.     constructor Create(AOwner: TComponent); Override;
  57.     procedure ReadRegistry;
  58.     procedure WriteRegistry;
  59.     procedure AddToList(NewItem: String);
  60.   published
  61.     property Key: String
  62.              read FKey write FKey;
  63.     property MaxHistoryLength: Integer
  64.              read FMaxHistoryLength write FMaxHistoryLength default 9;
  65.     property RootKey: TRootKey read FRootKey write SetRootKey default hkCurrentUser;
  66.  
  67.   end;
  68.  
  69.  
  70. procedure Register;
  71.  
  72. const
  73. RootKeyValues: array[TRootKey] of integer =
  74. (HKey_Classes_Root, HKey_Current_Config, HKey_Current_User, HKey_Dyn_Data,
  75.               HKey_Local_Machine, HKey_Users);
  76.  
  77. implementation
  78.  
  79.  
  80. constructor THistoryComboBox.Create(AOwner: TComponent);
  81. begin
  82.   inherited Create(AOwner);
  83.   FMaxHistoryLength := 9;
  84.   FRootKey := hkCurrentUser;
  85. end;
  86.  
  87. procedure THistoryComboBox.SetRootKey(ARootKey: TRootKey);
  88. begin
  89.   if FRootKey <> ARootKey then
  90.      FRootKey := ARootKey;
  91. end;
  92.  
  93. procedure THistoryComboBox.ReadRegistry;
  94. { Reads items from Registry }
  95. var
  96.   Registry: TRegistry;
  97.   i : Integer;
  98.   FileName, Value: String;
  99. begin
  100.   if FKey <> '' then begin
  101.  
  102.     Registry := TRegistry.Create;
  103.     try
  104.       Clear;
  105.       { Read maximum allowed entries from Registry }
  106.       Registry.RootKey := RootKeyValues[FRootKey];
  107.       if Registry.OpenKey('\'+FKey,False) then begin
  108.         for i:= 0 to FMaxHistoryLength-1 do begin
  109.            Value := Registry.ReadString('H'+IntToStr(i));
  110.            If Value <> ''
  111.              then Items.Add(Value)
  112.              else break;
  113.         end;
  114.       end;
  115.     finally
  116.       Registry.Free;
  117.     end;
  118.  
  119.   end;
  120. end;
  121.  
  122.  
  123. procedure THistoryComboBox.WriteRegistry;
  124. { Writes items to registry }
  125. var
  126.   Registry: TRegistry;
  127.   FileName: String;
  128.   i : Integer;
  129. begin
  130.   if FKey <> '' then begin
  131.  
  132.     Registry := TRegistry.Create;
  133.     try
  134.       { Read maximum allowed entries from Registry }
  135.       Registry.RootKey :=  RootKeyValues[FRootKey];
  136.       if Registry.OpenKey('\'+FKey,False) then begin
  137.         try
  138.           for i:= 0 to FMaxHistoryLength-1 do
  139.              Registry.ReadString('H'+IntToStr(i));
  140.         except
  141.           on ERegistryException do;
  142.         end;
  143.       end;
  144.  
  145.       { Erase existing entries to get rid of more then the max. allowed entries }
  146.       if Registry.OpenKey('\',False) then
  147.         Registry.DeleteKey('FKey');
  148.       { Add to Historylistbox }
  149.       AddToList(Text);
  150.       { Write to Registry }
  151.       if Registry.OpenKey('\'+FKey,True) then
  152.       for i := 0 to Items.Count-1 do
  153.          Registry.WriteString('H'+IntToStr(i),Items[i]);
  154.     finally
  155.       Registry.Free;
  156.     end;
  157.  
  158.   end;
  159. end;
  160.  
  161.  
  162. procedure THistoryComboBox.AddToList(NewItem: String);
  163. { Add a string to the Historylistbox }
  164. var
  165.    i: integer;
  166. begin
  167.    if NewItem <> '' then begin
  168.      { Insert in first position }
  169.      Items.Insert(0,NewItem);
  170.      { Check maximum numer of entries and delete any duplicate }
  171.      for i := 1 to Items.Count-1 do
  172.        if (Items[i] = NewItem) or (i > FMaxHistoryLength-1) then
  173.          Items.Delete(i);
  174.    end;
  175. end;
  176.  
  177.  
  178. procedure Register;
  179. begin
  180.   RegisterComponents('SupplΘment',[THistoryComboBox]);
  181. end;
  182.  
  183. end.
  184.  
  185. 
  186.